/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thynk
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddInventoryDialog extends JDialog{
    private JPanel bottom;
    private JButton okButton, cancelButton;
    private AddInventoryPanel dialogPanel;
    private final int Width = 500;
    private final int Height = 300;

    public AddInventoryDialog(JFrame owner) {
        super(owner, true);
        setTitle("Add New Inventory");

        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        ButtonHandler handler = new ButtonHandler();
        okButton.addActionListener(handler);
        cancelButton.addActionListener(handler);

        bottom = new JPanel();
        bottom.add(okButton);
        bottom.add(cancelButton);
        bottom.setBorder(BorderFactory.createEtchedBorder());
        dialogPanel = new AddInventoryPanel();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(bottom, BorderLayout.SOUTH);
        getContentPane().add(dialogPanel, BorderLayout.CENTER);

        setSize(Width,Height);
    }

    class ButtonHandler implements ActionListener {
        public void actionPerformed (ActionEvent evt) {
            JButton button = (JButton) evt.getSource();
            String label = button.getText();

            if("OK".equals(label)) {
                // add inventory here
                setInventory();
            }

            dialogPanel.reset();
            setVisible(false);
        }
    }

    private void setInventory() {
        Inventory temp = new Inventory(dialogPanel.nameField.getText());
        temp.setInvOnHand(Integer.parseInt(dialogPanel.quantityField.getText()));
        SupplyChainManagement.inv.add(temp);
        SupplyChainManagement.writeProduct();
    }
}
